﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;

namespace ReadFilesFromDirectory
{
    public partial class AdminDirectoryUtility : Form
    {
        private System.Windows.Forms.Timer timer;
        private string[] arrayOfFiles;
        private int count;
        private bool isStreaming;

        public AdminDirectoryUtility()
        {
            isStreaming = false;
            count = 0;
            InitializeComponent();
        }

        private void browseButton_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folderBrowserDialog1 = new FolderBrowserDialog();

            if(timer!=null)
                timer.Stop();

            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK) 
            {
                count = 0;
                if (timer != null)
                {
                    changeButton(false);
                }
                timer = null;
                textBox_directory.Text = folderBrowserDialog1.SelectedPath; 
            }
        }

        private void cancel_button_Click(object sender, EventArgs e)
        {
            this.Owner.Enabled = true;
            this.Dispose();
        }

        public Boolean closeForm()
        {
            DialogResult diaRslt = MessageBox.Show("Do you really want to exit?.", "Exit Conformation", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (diaRslt == DialogResult.Yes) { return true; }
            else { return false; }
        }

        private void AdminDirectoryUtility_FormClosing(object sender, FormClosingEventArgs e)
        {
           // if (closeForm())
           // {
                e.Cancel = false;
                this.Owner.Enabled = true;
          //  }
          //  else { e.Cancel = true; }
        }
        
        private void changeButton(Boolean streaming)
        {
            if (isStreaming)
            {
                button2.Text = "Stop Streaming";
                if (timer != null)
                {
                    timer.Start();
                }
            }
            else
            {
                button2.Text = "Start Streaming";
                timer.Stop();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            isStreaming = !isStreaming;
            changeButton(isStreaming);

            InitializeTimer();
        }

        private void showImage(object sender, EventArgs e)
        {
            if (count == arrayOfFiles.Length - 1)
            {
                count = 0;
            }
            else
            {
                count++;
            }
            string fileName = arrayOfFiles[count];
            Bitmap img;
            Image im = Image.FromFile(fileName);
            img = (Bitmap)im;
            pictureBox1.Image = img;
        }

        private void InitializeTimer()
        {
            if (timer == null)
            {
                string directoryName = @textBox_directory.Text;
                arrayOfFiles = Directory.GetFiles(directoryName);
                timer = new System.Windows.Forms.Timer();
                timer.Tick += new EventHandler(showImage);                // Everytime timer ticks, timer_Tick will be called
                timer.Interval = (500) * (1);                             // Timer will tick 2 times every second
                timer.Enabled = true;
            }// Enable the timer
        }
    }
 }
